<?php
namespace App\Http\Controllers\ApiAdmin;

use App\Service\Admin\DrugService;
use Illuminate\Http\Request;

class DrugController extends BaseController
{
    //药物

    public function __construct(DrugService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 修改
     */
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['img'] = $request->post('img', '');
        $pas['price'] = $request->post('price', '');
        $pas['price'] = $pas['price'] ? $pas['price'] * 100 : 0;
        $pas['num'] = $request->post('num', '');
//        $pas['symptom'] = $request->post('symptom', '');
        $tagStr = $request->post('symptom', '');
        $pas['zhuzhi'] = $request->post('zhuzhi', '');
        $pas['yongfa'] = $request->post('yongfa', '');
        $pas['period'] = $request->post('period', '');
        $pas['instruction'] = $request->post('instruction', '');
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '分类必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '药品名称必须的');
        }
        if (!$pas['num']) {
            return $this->toJson(-2, '数量必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas, $tagStr);
        } else {
            $res = $this->db->add($pas, $tagStr);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 删除
     */
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 上下架
     */
    public function shelfModify(Request $request)
    {
        $id = $request->post('id', 0);
        $shelf = $request->post('shelf', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->shelfModify($id, $shelf);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 列表
     */
    public function cateIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->cateIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 修改
     */
    public function cateModify(Request $request)
    {
        $id = $request->post('id', 0);
        $title = $request->post('title', '');
        if (!$title) {
            return $this->toJson(-2, '分类名称必须的');
        }
        if ($id) {
            $res = $this->db->cateModify($id, $title);
        } else {
            $res = $this->db->cateAdd($title);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 删除
     */
    public function cateDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->cateDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 列表
     */
    public function tagIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $drug_id = $request->post('drug_id', 0);
        $res = $this->db->tagIndex($pages, $drug_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 修改
     */
    public function tagModify(Request $request)
    {
        $drug_id = $request->post('drug_id', 0);
        $title = $request->post('title', '');
        if (!$title) {
            return $this->toJson(-2, '分类名称必须的');
        }
        $res = $this->db->tagAdd($drug_id, $title);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 删除
     */
    public function tagDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->tagDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}